package com.vm.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridView;

/**
 * 
 * @ProjectName: [WeiMai]
 * @Author: [kent.wang]
 * @CreateDate: [2015年1月31日 上午9:47:13]
 * @Version: [v1.0]
 * 
 */
public class CalendarGridView extends GridView {
	public CalendarGridView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public CalendarGridView(Context context) {
		super(context);
	}

	public CalendarGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
				MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, expandSpec);
	}

	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		if (getChildAt(0) != null) {
			View localView1 = getChildAt(0);
			int column = getWidth() / localView1.getWidth();
			int childCount = getChildCount();
			if (childCount % column == 0) {
			} else {
			}
			Paint localPaint, localPaint2;
			localPaint = new Paint();
			localPaint2 = new Paint();
			localPaint.setStyle(Paint.Style.STROKE);
			localPaint2.setStyle(Paint.Style.STROKE);
			localPaint.setStrokeWidth(1);
			localPaint2.setStrokeWidth(1);
			localPaint.setColor(Color.parseColor("#bbcedc"));
			localPaint2.setColor(Color.parseColor("#bbcedc"));
			for (int i = 0; i < childCount; i++) {
				View cellView = getChildAt(i);
				// 每个item四周的边框
				canvas.drawLine(cellView.getLeft(), cellView.getTop(),
						cellView.getLeft(), cellView.getBottom(), localPaint);
				canvas.drawLine(cellView.getLeft(), cellView.getTop(),
						cellView.getRight(), cellView.getTop(), localPaint);
				canvas.drawLine(cellView.getRight(), cellView.getTop(),
						cellView.getRight(), cellView.getBottom(), localPaint);
				canvas.drawLine(cellView.getLeft(), cellView.getBottom(),
						cellView.getRight(), cellView.getBottom(), localPaint);

				canvas.drawLine(cellView.getRight() - 1, cellView.getTop(),
						cellView.getRight() - 1, cellView.getBottom(),
						localPaint2);
				canvas.drawLine(cellView.getLeft() - 1, cellView.getTop(),
						cellView.getLeft() - 1, cellView.getBottom(),
						localPaint2);
				canvas.drawLine(cellView.getRight(), cellView.getBottom() - 1,
						cellView.getLeft(), cellView.getBottom() - 1,
						localPaint2);
				canvas.drawLine(cellView.getRight(), cellView.getTop() - 1,
						cellView.getLeft(), cellView.getTop() - 1,
						localPaint2);
				if (i < 7) {
					canvas.drawLine(cellView.getLeft(), cellView.getTop() + 1,
							cellView.getRight(), cellView.getTop() + 1,
							localPaint);
					canvas.drawLine(cellView.getLeft(), cellView.getTop() + 2,
							cellView.getRight(), cellView.getTop() + 2,
							localPaint);
					canvas.drawLine(cellView.getLeft(), cellView.getTop() + 3,
							cellView.getRight(), cellView.getTop() + 3,
							localPaint);
					canvas.drawLine(cellView.getLeft(), cellView.getTop() + 4,
							cellView.getRight(), cellView.getTop() + 4,
							localPaint);
				}
				if (i % 7 == 0) {
					canvas.drawLine(cellView.getLeft() + 1, cellView.getTop(),
							cellView.getLeft() + 1, cellView.getBottom(),
							localPaint);
					canvas.drawLine(cellView.getLeft() + 2, cellView.getTop(),
							cellView.getLeft() + 2, cellView.getBottom(),
							localPaint);
					canvas.drawLine(cellView.getLeft() + 3, cellView.getTop(),
							cellView.getLeft() + 3, cellView.getBottom(),
							localPaint);
					canvas.drawLine(cellView.getLeft() + 4, cellView.getTop(),
							cellView.getLeft() + 4, cellView.getBottom(),
							localPaint);
				}
				if ((i + 1) % 7 == 0) {
					canvas.drawLine(cellView.getRight() - 1, cellView.getTop(),
							cellView.getRight() - 1, cellView.getBottom(),
							localPaint);
					canvas.drawLine(cellView.getRight() - 2, cellView.getTop(),
							cellView.getRight() - 2, cellView.getBottom(),
							localPaint);
					canvas.drawLine(cellView.getRight() - 3, cellView.getTop(),
							cellView.getRight() - 3, cellView.getBottom(),
							localPaint);
					canvas.drawLine(cellView.getRight() - 4, cellView.getTop(),
							cellView.getRight() - 4, cellView.getBottom(),
							localPaint);
				} else {
					canvas.drawLine(cellView.getRight() + 1, cellView.getTop(),
							cellView.getRight() + 1, cellView.getBottom() + 3,
							localPaint2);
				}
				if ((childCount - i) < 8) {
					canvas.drawLine(cellView.getLeft(),
							cellView.getBottom() - 1, cellView.getRight(),
							cellView.getBottom() - 1, localPaint);
					canvas.drawLine(cellView.getLeft(),
							cellView.getBottom() - 2, cellView.getRight(),
							cellView.getBottom() - 2, localPaint);
					canvas.drawLine(cellView.getLeft(),
							cellView.getBottom() - 3, cellView.getRight(),
							cellView.getBottom() - 3, localPaint);
					canvas.drawLine(cellView.getLeft(),
							cellView.getBottom() - 4, cellView.getRight(),
							cellView.getBottom() - 4, localPaint);
				} else {
					canvas.drawLine(cellView.getRight(),
							cellView.getBottom() + 1, cellView.getLeft() - 1,
							cellView.getBottom() + 1, localPaint2);
				}

			}

		}
	}
}
